package com.tale.springbootatm.common.rabbit.consumer;

import com.rabbitmq.client.Channel;
import com.tale.springbootatm.common.constant.RabbitConstant;
import com.tale.springbootatm.common.util.MailUtil;
import com.tale.springbootatm.common.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * 消费者：用于读取队列的消息
 *
 * author: 蔡奇峰
 * date: 2020/5/2 12:57
 **/
@Component
@Slf4j
public class MqConsumer {

    @Autowired
    private MailUtil mailUtil;


    /**
     * 监听mail队列消息，当mail队列有消息时进行消费
     *
     * @Payload 注解将消息强制转换为Message对象
     * @Headers 注解获取消息头信息
     *
     * @param msg
     * @param channel
     * @param headers
     */
    @RabbitListener(queues = RabbitConstant.MAIL_QUEUE)
    public void receiver(@Payload MessageVO msg, Channel channel,
                         @Headers Map<String, Object> headers){

        mailUtil.sendUpdatePwd(msg);
        log.info("发送邮件......");
//        Message msg = null;
//        if (obj instanceof Message){
//            msg = (Message) obj;
//
//            mailUtil.sendUpdatePwd(msg);
//            log.info("发送邮件......");
//        }

    }

}
